<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>地图API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 4px;
            white-space: pre-wrap;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        input {
            margin: 5px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>地图API功能测试</h1>
        
        <!-- 获取所有国家 -->
        <div class="test-section">
            <h3>1. 获取所有国家</h3>
            <button onclick="testGetCountries()">测试获取国家列表</button>
            <div id="countries-result" class="result"></div>
        </div>

        <!-- 根据国家获取港口 -->
        <div class="test-section">
            <h3>2. 根据国家获取港口</h3>
            <input type="text" id="country-input" placeholder="输入国家名称，如：中国" value="中国">
            <button onclick="testGetPortsByCountry()">测试获取港口</button>
            <div id="ports-result" class="result"></div>
        </div>

        <!-- 搜索港口 -->
        <div class="test-section">
            <h3>3. 搜索港口</h3>
            <input type="text" id="search-input" placeholder="输入港口名称，如：上海" value="上海">
            <button onclick="testSearchPorts()">测试搜索港口</button>
            <div id="search-result" class="result"></div>
        </div>

        <!-- 获取附近港口 -->
        <div class="test-section">
            <h3>4. 获取附近港口</h3>
            <input type="number" id="lat-input" placeholder="纬度" value="31.2304" step="any">
            <input type="number" id="lng-input" placeholder="经度" value="121.4737" step="any">
            <input type="number" id="radius-input" placeholder="半径(km)" value="100">
            <button onclick="testGetNearbyPorts()">测试获取附近港口</button>
            <div id="nearby-result" class="result"></div>
        </div>

        <!-- 港口收藏功能 -->
        <div class="test-section">
            <h3>5. 港口收藏功能</h3>
            <input type="number" id="user-id-input" placeholder="用户ID" value="1">
            <input type="number" id="port-id-input" placeholder="港口ID" value="1">
            <button onclick="testAddFavorite()">添加收藏</button>
            <button onclick="testRemoveFavorite()">取消收藏</button>
            <button onclick="testGetFavorites()">获取收藏列表</button>
            <div id="favorite-result" class="result"></div>
        </div>
    </div>

    <script>
        const BASE_URL = 'http://localhost:6000';

        async function makeRequest(url, options = {}) {
            try {
                const response = await fetch(url, {
                    ...options,
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    }
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }
                
                return await response.json();
            } catch (error) {
                throw error;
            }
        }

        function displayResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.className = `result ${isError ? 'error' : 'success'}`;
            element.textContent = JSON.stringify(data, null, 2);
        }

        async function testGetCountries() {
            try {
                const data = await makeRequest(`${BASE_URL}/map/countries`);
                displayResult('countries-result', data);
            } catch (error) {
                displayResult('countries-result', { error: error.message }, true);
            }
        }

        async function testGetPortsByCountry() {
            const country = document.getElementById('country-input').value;
            if (!country) {
                displayResult('ports-result', { error: '请输入国家名称' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/ports/country?country=${encodeURIComponent(country)}`);
                displayResult('ports-result', data);
            } catch (error) {
                displayResult('ports-result', { error: error.message }, true);
            }
        }

        async function testSearchPorts() {
            const keyword = document.getElementById('search-input').value;
            if (!keyword) {
                displayResult('search-result', { error: '请输入搜索关键词' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/ports/search?keyword=${encodeURIComponent(keyword)}`);
                displayResult('search-result', data);
            } catch (error) {
                displayResult('search-result', { error: error.message }, true);
            }
        }

        async function testGetNearbyPorts() {
            const lat = document.getElementById('lat-input').value;
            const lng = document.getElementById('lng-input').value;
            const radius = document.getElementById('radius-input').value;

            if (!lat || !lng || !radius) {
                displayResult('nearby-result', { error: '请输入完整的位置信息' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/ports/nearby?lat=${lat}&lng=${lng}&radius=${radius}`);
                displayResult('nearby-result', data);
            } catch (error) {
                displayResult('nearby-result', { error: error.message }, true);
            }
        }

        async function testAddFavorite() {
            const userId = document.getElementById('user-id-input').value;
            const portId = document.getElementById('port-id-input').value;

            if (!userId || !portId) {
                displayResult('favorite-result', { error: '请输入用户ID和港口ID' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/favorites/add`, {
                    method: 'POST',
                    body: JSON.stringify({ userId: parseInt(userId), portId: parseInt(portId) })
                });
                displayResult('favorite-result', data);
            } catch (error) {
                displayResult('favorite-result', { error: error.message }, true);
            }
        }

        async function testRemoveFavorite() {
            const userId = document.getElementById('user-id-input').value;
            const portId = document.getElementById('port-id-input').value;

            if (!userId || !portId) {
                displayResult('favorite-result', { error: '请输入用户ID和港口ID' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/favorites/remove?userId=${userId}&portId=${portId}`, {
                    method: 'DELETE'
                });
                displayResult('favorite-result', data);
            } catch (error) {
                displayResult('favorite-result', { error: error.message }, true);
            }
        }

        async function testGetFavorites() {
            const userId = document.getElementById('user-id-input').value;

            if (!userId) {
                displayResult('favorite-result', { error: '请输入用户ID' }, true);
                return;
            }

            try {
                const data = await makeRequest(`${BASE_URL}/map/favorites?userId=${userId}`);
                displayResult('favorite-result', data);
            } catch (error) {
                displayResult('favorite-result', { error: error.message }, true);
            }
        }

        // 页面加载完成后自动测试获取国家列表
        window.onload = function() {
            console.log('页面加载完成，可以开始测试地图API功能');
        };
    </script>
</body>
</html> 